﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using ThirdPartyNinjas.XnaUtility;
using OOAD_Project_Engine.Application.Services;
using OOAD_Project_Engine.Foundation;
using Microsoft.Xna.Framework;

namespace OOAD_Project_Editor.Foundation
{
    sealed public class XmlTexture
    {
        bool loadSuccess = true;
        public XmlTexture(string source)
        {
            this.SourcePath = source;

            try
            {
                Texture2D = GameHelper.GetService<IContentService>().Load<Texture2D>(source);
                TextureAtlas = GameHelper.GetService<IContentService>().Load<TextureAtlas>(source + "Xml");
            }
            catch (Exception)
            {
                loadSuccess = false;
                //Texture2D = new Texture2D(GameHelper.GetService<IGraphicService>().GraphicsDevice,64,64);
            }

        }

        public string SourcePath { get; private set; }

        public Texture2D Texture2D { get; private set; }

        public TextureAtlas TextureAtlas { get; private set; }

        public TextureRegion GetRegion(string id)
        {
            if (loadSuccess)
                return TextureAtlas.GetRegion(id);
            else {
                TextureRegion region = new TextureRegion();
                region.Bounds = new Microsoft.Xna.Framework.Rectangle();
                region.OriginBottomRight = Vector2.Zero;
                region.OriginCenter = Vector2.Zero;
                region.OriginTopLeft = Vector2.Zero;
                region.Rotated = false;
                return region;
            }
        }
    }
}
